package com.bfly.mall.admin.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.core.security.Md5PwdEncoder;
import com.bfly.mall.admin.dao.AdminMapper;
import com.bfly.mall.admin.entity.Admin;
import com.bfly.mall.admin.dto.AdminFormDTO;
import com.bfly.mall.admin.service.IAdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/10/14 16:30
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class AdminServiceImpl extends BaseServiceImpl<Admin, Integer> implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin getAdmin(String userName) {
        Admin admin = adminMapper.getAdmin(userName);
        return admin;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AdminFormDTO formDTO) {
        Admin admin = getAdmin(formDTO.getUserName());
        if (admin != null) {
            throw new ServiceResponseException(SysError.DATA_REPEAT, "用户名已重复!");
        }

        admin = new Admin();
        BeanUtils.copyProperties(formDTO, admin);
        admin.setPassword(new Md5PwdEncoder().encodePassword(admin.getPassword()));
        admin.setUpdateDate(new Date());
        admin.setCreateDate(admin.getUpdateDate());
        admin.setEnable(true);
        admin.setLastLoginIp("0.0.0.0");
        admin.setLastLoginDate(new Date());
        super.save(admin);

        // 绑定角色信息
        final int adminId = admin.getId();
        formDTO.getRoleIds().forEach(roleId -> {
            adminMapper.bindRole(adminId, roleId);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(AdminFormDTO formDTO) {
        Admin admin = getAdmin(formDTO.getUserName());
        if (admin == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "管理员信息不存在!");
        }

        admin.setRealName(formDTO.getRealName());
        admin.setPhone(formDTO.getPhone());
        admin.setUpdateDate(new Date());
        admin.setCreateDate(admin.getUpdateDate());
        super.edit(admin);

        // 绑定角色信息 先清空然后再绑定
        final int adminId = admin.getId();
        adminMapper.clearRole(adminId);
        formDTO.getRoleIds().forEach(roleId -> {
            adminMapper.bindRole(adminId, roleId);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Integer... adminIds) {
        for (Integer adminId : adminIds) {
            // 先回收管理员的权限 然后删除
            adminMapper.clearRole(adminId);
        }
        return super.remove(adminIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editPwd(String userName, String password, String newPassword) {
        Admin admin = getAdmin(userName);
        if (admin == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在该管理员信息!");
        }
        if (!admin.getPassword().equals(new Md5PwdEncoder().encodePassword(password))) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "原密码不正确!");
        }
        admin.setPassword(new Md5PwdEncoder().encodePassword(newPassword));
        super.edit(admin);
    }

    @Override
    public String bindAdminNotify(String userName) {
        return null;
    }
}
